{extend name="public/base"/}
{block name="style"}
<style>
    .layui-table th{text-align:center;}
    .layui-table td{text-align:center;}
    .text-left{text-align: left !important;}
</style>
{/block}
{block name="content"}
<div class="data-list" data-url="{:url('index')}">
    <form class="layui-form inline-form">
        <div class="pull-left">
            <div class="layui-inline">
                <input type="text" name="sn" autofocus placeholder="采购单号/供应商单号" class="layui-input input-order-sn">
            </div>
            <div class="layui-inline">
                <input type="text" placeholder="交货日期" name="delivery_date" class="layui-input laydate-range">
            </div>
            <div class="layui-inline">
                <select name="warehouse_id" lay-filter="data-list" lay-search>
                    <option value="">调入仓库</option>
                    {foreach $warehouse as $w}
                    <option value="{$w.warehouse_id}">{$w.name}</option>
                    {/foreach}
                </select>
            </div>
            <div class="layui-inline">
                <select name="check_status" lay-filter="data-list" lay-search>
                    <option value="">审批状态</option>
                    {foreach $check_status as $k => $s}
                    <option value="{$k}">{$s}</option>
                    {/foreach}
                </select>
            </div>
            <div class="layui-inline">
                <select name="order_status" lay-filter="data-list" lay-search>
                    <option value="">采购单状态</option>
                    {foreach $order_status as $k => $s}
                    <option value="{$k}">{$s}</option>
                    {/foreach}
                </select>
            </div>
            <div class="layui-inline">
                <select name="pay_status" lay-filter="data-list" lay-search>
                    <option value="">付款状态</option>
                    {foreach $pay_status as $k => $s}
                    <option value="{$k}">{$s}</option>
                    {/foreach}
                </select>
            </div>
            <div class="layui-inline">
                <select name="producer_id" lay-filter="data-list" lay-search>
                    <option value="">供应商</option>
                    {foreach $producer as $p}
                    <option value="{$p.producer_id}">{$p.name}</option>
                    {/foreach}
                </select>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-sm layui-btn-normal search"><i class="layui-icon layui-icon-search"></i></button>
            </div>
        </div>
    </form>
    <div class="layui-row form-lists-button">
        <div class="layui-inline layui-btn-group">
            <a class="layui-btn layui-btn-normal layui-btn-sm" href="{:url('add')}"><i class="layui-icon layui-icon-add-circle"></i>创建采购单</a>
            <a id="batch-check" target="purchase-id" msg="请选择要审批的采购单" class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon layui-icon-auz"></i>
                批量审批</a>
            <a id="batch-generate-order" class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon layui-icon-share"></i>批量下单</a>
            <a id="batch-cancel" class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon layui-icon-logout"></i>批量取消</a>
            <a type="button" id="batch-print"  class="layui-btn layui-btn-sm layui-btn-normal"><i
                    class="layui-icon layui-icon-print"></i>打印采购单
            </a>
            {if $show_verification}
            <a id="batch-verification" target="purchase-id" msg="请选择要核销的采购单" class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon layui-icon-auz"></i>
                批量核销</a>
            {/if}
        </div>
    </div>
    <div class="data">
        <p><i class="fa fa-spinner fa-spin"></i> 加载中...</p>
    </div>
</div>
{include file="pur/purchase/check" /}
{/block}
{block name="script"}
<link rel="stylesheet" href="__PUBLIC__/css/print.min.css">
<script type="text/javascript" src="__PUBLIC__/js/print.min.js"></script>
<script type="text/javascript">
layui.use(['layer', 'jquery'], function()
{
    var layer = layui.layer
        , $ = layui.jquery;

    $("body").on("change", ".input-producer-sn", function()
    {
        var $this = $(this)
            ,sn = $.trim($this.val());
        if(!sn) return false;
        var pid = $this.parent().parent().find(".purchase-id").val();
        if(!pid) return false;
        $this.val('');
        layer.load(0, {shade: false});
        $.get("{:url('producer_sn')}", {producer_sn: sn, purchase_id: pid}).done(function(response)
        {
            layer.closeAll('loading');
            layer.msg(response.msg, {icon: response.code == 1 ? 6 : 5, time: 1500});
            if(response.code == 1) $this.parent().html(sn);
        });
    }).on("keydown",".input-producer-sn",function(event){if(event.keyCode==13)$(this).change()});

    // 批量下单
    $("#batch-generate-order").click(function()
    {
        var ids = getIds('.purchase-id');
        if(ids.length < 1)
        {
            layer.msg("请选择要下单的采购单");
            return false;
        }
        layer.load(0, {shade: false});
        var $this = $(this);
        generateOrder(ids, function(response)
        {
            if(response.code == 1) $this.closest('.data-list').getList();
        });
    });

    // 批量生成付款单
    $("#batch-generate-payment").click(function()
    {
        var ids = getIds('.purchase-id');
        if(ids.length < 1)
        {
            layer.msg("请选择要生成的采购单");
            return false;
        }
        generatePayment(ids, function()
        {
            $(".data").closest('.data-list').getList();
        });
    });

    // 取消采购单
    $("body").on("click", ".pur-cancel, #batch-cancel", function()
    {
        var purId = $(this).is("#batch-cancel")
            ? getIds('.purchase-id').join(",")
            : $(this).attr("item");
        if(purId.length < 1)
        {
            layer.msg("请选择要取消的采购单");
            return false;
        }
        cancelPurchase(purId, function()
        {
            $(".data").closest('.data-list').getList();
        });
    });

    // 打印采购单
    $("#batch-print").click(function()
    {
        var ids = getIds(".purchase-id");
        if(ids.length < 1)
        {
            layer.msg("请选择要打印的采购单");
            return false;
        }
        layer.load(0, {shade: false});
        $.get("{:url('print')}", {ids: ids.join(',')}).done(function(response)
        {
            layer.closeAll('loading');
            if(response.code != 1)
            {
                layer.msg(response.msg);
                return false;
            }
            printJS({printable: response.data.file, type: 'pdf', base64: true, showModal:true})
        });
    });

    $("#batch-verification").click(function()
    {
        var ids = getIds(".purchase-id");
        if(ids.length < 1)
        {
            layer.msg("请选择要核销的采购单");
            return false;
        }
        layer.load(0, {shade: false});
        $.get("{:url('verification')}", {ids: ids.join(',')}).done(function(response)
        {
            layer.closeAll('loading');
            layer.msg(response.msg, {icon: response.code == 1 ? 6 : 5, time: 1500});
            if(response.code != 1) return false;
            $(".data").closest('.data-list').getList();
        });
    });
});
</script>
{/block}